package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final OrderMapper orderMapper;

    private final UserMapper userMapper;

    @Override
    public TurnoverReportVO statistics(LocalDate begin, LocalDate end) {
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        //存放传入日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        String dates = StringUtils.join(dateList, ",");


        //存放营业额
        List<Double> turnoverList = new ArrayList<>();
        for(LocalDate date:dateList){

            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);

            Map map = new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);

            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0:turnover;
            turnoverList.add(turnover);
            String turnovers = StringUtils.join(turnoverList,",");


            turnoverReportVO.setDateList(dates);
            turnoverReportVO.setTurnoverList(turnovers);

        }
        return turnoverReportVO;
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        UserReportVO userReportVO = new UserReportVO();
        //先存取日期数据
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        String dates = StringUtils.join(dateList, ",");
        //存取用户人数数据
        List<Double> userNumber = new ArrayList<>();
        List<Double> newUserNumber = new ArrayList<>();
        for(LocalDate date : dateList){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);

            Double users = userMapper.sumUsers(endTime);
            Double newUsers = userMapper.sumNewUsers(beginTime,endTime);

            userNumber.add(users);
            newUserNumber.add(newUsers);

        }
        String users = StringUtils.join(userNumber,",");
        String newUsers = StringUtils.join(newUserNumber,",");
        userReportVO.setDateList(dates);
        userReportVO.setNewUserList(newUsers);
        userReportVO.setTotalUserList(users);
        return  userReportVO;
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        OrderReportVO orderReportVO = new OrderReportVO();
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        String dates = StringUtils.join(dateList, ",");

        List<Double> orderTotals = new ArrayList<>();
        List<Double> trueOrderTotals = new ArrayList<>();

        Integer totalOrderCount = 0;
        Integer validOrderCount = 0;

        for(LocalDate date:dateList){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);

            //查询每天的订单总数
            Double orderTotal = orderMapper.sumOrder(beginTime,endTime);
            //查询每天的有效订单数
            Integer status = Orders.COMPLETED;
            Double trueOrderTotal = orderMapper.sumTrueOrder(beginTime,endTime,status);

            orderTotals.add(orderTotal);
            trueOrderTotals.add(trueOrderTotal);

            totalOrderCount = (int) (totalOrderCount+orderTotal);
            validOrderCount = (int) (validOrderCount+trueOrderTotal);
        }
        String order = StringUtils.join(orderTotals, ",");
        String trueorder = StringUtils.join(trueOrderTotals, ",");
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            orderCompletionRate = (double) (validOrderCount/totalOrderCount);
        }

        orderReportVO.setDateList(dates);
        orderReportVO.setOrderCountList(order);
        orderReportVO.setOrderCompletionRate(orderCompletionRate);
        orderReportVO.setValidOrderCount(validOrderCount);
        orderReportVO.setValidOrderCountList(trueorder);
        orderReportVO.setTotalOrderCount(totalOrderCount);


        return orderReportVO;
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end,LocalTime.MAX);

        List<GoodsSalesDTO> salesTop = orderMapper.getSalesTop(beginTime, endTime);

        List<String> names = salesTop.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");
        List<Integer> numbers = salesTop.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(numbers, ",");

        salesTop10ReportVO.setNameList(nameList);
        salesTop10ReportVO.setNumberList(numberList);

        return salesTop10ReportVO;


    }


}
